package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.SysPropertyConfigMapper;
import com.fc.mall.model.SysPropertyConfig;
import com.fc.mall.model.SysPropertyConfigExample;
import com.fc.mall.portal.service.SysPropertyConfigService;
import com.fc.mall.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysPropertyConfigServiceImpl implements SysPropertyConfigService {

    @Autowired
    private SysPropertyConfigMapper propertyConfigMapper;

    /**
     * 推荐人时是否临时锁定粉丝, 1->需要暂时锁定，到时间后未购物变为游离会员 0->不需要暂时锁定, 直接锁定推荐人
     * @return
     */
    public Integer getLockReferer()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.LockReferer.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return Integer.valueOf(config.getValue());
            }
        }
        return Integer.valueOf(Constants.SysPropertyConfigName.LockReferer.getValue());
    }

    /**
     * 拉新时临时锁定时间, 锁定时长, 单位小时
     * @return
     */
    public Integer getLockDuration()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.LockDuration.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return Integer.valueOf(config.getValue());
            }
        }
        return Integer.valueOf(Constants.SysPropertyConfigName.LockDuration.getValue());
    }

    /**
     * 推荐人/上级代理商分佣百分比
     * @return
     */
    public Integer getRefererRate()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.RefererRate.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return Integer.valueOf(config.getValue());
            }
        }
        return Integer.valueOf(Constants.SysPropertyConfigName.RefererRate.getValue());
    }

    /**
     * 上级的上级代理商分佣百分比
     * @return
     */
    public Integer getSuperRefererRate()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.SuperRefererRate.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return Integer.valueOf(config.getValue());
            }
        }
        return Integer.valueOf(Constants.SysPropertyConfigName.SuperRefererRate.getValue());
    }

    /**
     * 合伙人分佣百分比
     * @return
     */
    public Integer getPartnerRate()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.PartnerRate.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return Integer.valueOf(config.getValue());
            }
        }
        return Integer.valueOf(Constants.SysPropertyConfigName.PartnerRate.getValue());
    }

    /**
     * 获取平台资质图片地址
     * @return
     */
    public String getPlatformAptitude()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.PlatformAptitude.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return config.getValue();
            }
        }
        return Constants.SysPropertyConfigName.PlatformAptitude.getValue();
    }

    @Override
    public List<SysPropertyConfig> getListByCatgory(Integer category) {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCategoryEqualTo(category);
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        return propertyConfigList;
    }

    public String getPromoteShareImageBackgroundUrl()
    {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.shareImageBackground.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return config.getValue();
            }
        }
        return Constants.SysPropertyConfigName.shareImageBackground.getValue();
    }

    @Override
    public String getPromoteShareTitle() {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(Constants.SysPropertyConfigName.promoteShareTitle.getName());
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return config.getValue();
            }
        }
        return Constants.SysPropertyConfigName.promoteShareTitle.getValue();
    }

    @Override
    public String getSysConfigValueByKey(String key) {
        SysPropertyConfigExample example = new SysPropertyConfigExample();
        example.createCriteria().andCodeEqualTo(key);
        List<SysPropertyConfig> propertyConfigList = propertyConfigMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(propertyConfigList))
        {
            SysPropertyConfig config = propertyConfigList.get(0);
            if(config != null && !StringUtils.isEmpty(config.getValue()))
            {
                return config.getValue();
            }
        }
        return "";
    }

}
